﻿using System;
using System.Linq;
using BO.News;
using GUI.Extensions;
using BO.Infrastructure.StaticData;

namespace GUI.Controls.News
{
    public partial class EmbedThreads : System.Web.UI.UserControl
    {
        public int ZoneId = 0;
        private const string ControlPath = "/Controls/News/NewsZone2.ascx";


        private const int TopVisibleThread = 2;

        protected void Page_Load(object sender, EventArgs e)
        {
            var bo = CacheSettingExtensions.EnableCacheHtml
                         ? NewsBO_NoCache.GetInstance()
                         : NewsBO.GetInstance();

            var embedThreads = bo.GetEmbedThreads(TopVisibleThread, ZoneId, DefinedConstants.Thread.TypeIds.EmbedThread);
            var count = 0;

            if (embedThreads != null)
            {
                if (embedThreads.Threads != null && embedThreads.Threads.Length > 0)
                {
                    var holders = new[] { PlaceHolder1, PlaceHolder2 };
                    foreach (var thread in embedThreads.Threads)
                    {
                        var control = LoadControl(ControlPath) as NewsZone2;
                        if (control != null)
                        {
                            control.IsThread = true;
                            control.GetDataFromNoCacheLayer = true;
                            control.DisplayPublishedDate = false;
                            control.DisplayRelateNews = false;

                            control.Thread = thread.OwnerInfo;

                            switch (thread.TemplateId)
                            {
                                default:
                                    control.DisplayOtherNews = false;
                                    control.Top = 5;
                                    control.StyleType = 4;
                                    control.ImageWidth = 100;
                                    control.ImageHeight = 75;
                                    break;

                                case 2:
                                    control.DisplayOtherNews = true;
                                    control.Top = 1;
                                    control.TopOther = 5;
                                    control.StyleType = 5;
                                    control.ImageWidth = 300;
                                    control.ImageHeight = 225;
                                    break;
                                case 3:
                                    control.DisplayOtherNews = true;
                                    control.Top = 0;
                                    control.TopOther = 10;
                                    control.StyleType = 5;
                                    control.ImageWidth = 0;
                                    control.ImageHeight = 0;
                                    break;

                                case 4:
                                    control.DisplayOtherNews = true;
                                    control.Top = 1;
                                    control.TopOther = 3;
                                    control.StyleType = 5;
                                    control.ImageWidth = 300;
                                    control.ImageHeight = 225;
                                    control.DisplayOtherNewsAvatar = true;
                                    control.OtherImageWidth = 100;
                                    control.OtherImageHeight = 75;
                                    break;
                            }
                        }


                        if (control == null || count >= TopVisibleThread) continue;
                        foreach (var placeHolder in holders.Where(placeHolder => placeHolder.Controls.Count == 0))
                        {
                            placeHolder.Controls.Add(control);
                            count++;
                            break;
                        }
                    }
                    return;
                }
            }
            Visible = false;
        }
    }
}